VFG.EWall = function(viewer, options) {
	this.viewer = viewer;
	this.options = options;
	this.url = options.url;
	this.parentId = options.parentId || '';
	this.id = options.id;
	this.name = options.name || '';
	this.code = options.code || '';
	this.type = options.type || '';
	this.thing = options.thing || {};
	this.attribute = options.attribute || {};
	this.propertys = options.propertys || [];
	if(!this.thing.positions || this.thing.positions.length<2){
		return;
	}
	this.init();
}
VFG.EWall.prototype.init = function() {
	let _this = this;
	this.entity = this.viewer.entities.add({
		id : this.id,
		name : this.name,
		code : this.code,
		type : this.type,
		wall : {
			positions : this.thing.positions,
			material : this.getMaterial(),
	        maximumHeights: [30, 30, 30, 30, 30],
	        minimumHeights: [0, 0, 0, 0, 0]
		}
	});
};

VFG.EWall.prototype.updateName = function(name) {
	let _this = this;
	this.name=name;
};

VFG.EWall.prototype.updateCode = function(code) {
	let _this = this;
	this.code=code;
};

VFG.EWall.prototype.getMaterial = function() {
	return new Cesium.WallMaterialProperty(this.thing.color ? this.thing.color : Cesium.Color.GREEN, 1500,this.url);
}

VFG.EWall.prototype.getMaximumHeights = function() {
	let _this =this; 
	this.maximumHeights = Array(this.thing.positions.length / 2).fill(600);
    this.minimumHeights = Array(this.thing.positions.length / 2).fill(60);
	this.dayMaximumHeights = Array(minimumHeights.length).fill(600);
	return new Cesium.CallbackProperty(() => {
      for (let i = 0; i < _this.minimumHeights.length; i++) {
        _this.dayMaximumHeights[i] += _this.maximumHeights[i] * 0.004;
        if (_this.dayMaximumHeights[i] > _this.maximumHeights[i]) {
          _this.dayMaximumHeights[i] = _this.minimumHeights[i];
        }
      }
      return _this.dayMaximumHeights;
    }, false);
}

VFG.EWall.prototype.updatePositions = function(positions, isDynamic) {
	let _this = this;
	if (positions) {
		this.thing.positions = positions;
		if (this.entity) {
			if (isDynamic) {
				if(!(this.entity.wall.positions instanceof Cesium.CallbackProperty)){
					this.entity.wall.positions = new Cesium.CallbackProperty(
						function(time) {
							return _this.thing.positions
						}, false);
				}
			} else {
				this.entity.wall.positions = _this.thing.positions
			}
		}
	}
}

VFG.EWall.prototype.updateMaterial = function(color) {
	if(color){
		this.thing.color=color;
	}
	if(this.entity){
		this.entity.wall.material=this.getMaterial();
	}
}

VFG.EWall.prototype.updateProperty= function(property) {
	var _this = this;
	if(_this.propertys){
		for(let index in _this.propertys){
			if(property.id==_this.propertys[index].id){
				_this.propertys.splice(index,1,property);
				return;
			}
		}
		_this.propertys.push(property);
	}
}

VFG.EWall.prototype.removeProperty= function(id) {
	var _this = this;
	if(_this.propertys){
		for(let index in _this.propertys){
			if(id==_this.propertys[index].id){
				_this.propertys.splice(index,1);
				return;
			}
		}
	}
};

VFG.EWall.prototype.destroy = function() {
	let _this = this;
	if (this.entity) {
		this.viewer.entities.remove(this.entity);
	}
	delete this.entity
	delete this.options
	delete this.thing
	delete this.viewer
	return Cesium.destroyObject(this);
};